package com.cskaoyan.service;

import com.cskaoyan.mapper.TopicMapper;
import com.cskaoyan.model.bean.Topic;
import com.cskaoyan.model.bean.TopicExample;
import com.cskaoyan.model.bo.extend.TopicBo;
import com.cskaoyan.model.vo.extend.TopicVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author：Chunk
 * @Description:
 * @Date Created in 10:36 2021/4/10
 */
@Service
public class TopicServiceImpl implements TopicService{
    @Autowired
    TopicMapper topicMapper;

    @Override
    public TopicVo topicList(TopicBo topicBo) {

        PageHelper.startPage(topicBo.getPage(), topicBo.getLimit());
        TopicExample topicExample = new TopicExample();

        TopicExample.Criteria criteria = topicExample.createCriteria();
        if(topicBo.getTitle() != null && !"".equals(topicBo.getTitle())){
            criteria.andTitleLike("%" + topicBo.getTitle() + "%");
        }
        if(topicBo.getSubtitle() != null && !"".equals(topicBo.getSubtitle())){
            criteria.andSubtitleLike("%" + topicBo.getSubtitle() + "%");
        }
        topicExample.setOrderByClause(topicBo.getSort() + " " + topicBo.getOrder());
        List<Topic> topics = topicMapper.selectByExample(topicExample);

        PageInfo<Topic> topicPageInfo = new PageInfo<>(topics);
        long total = topicPageInfo.getTotal();
        TopicVo topicVo = new TopicVo();
        topicVo.setTotal(total);
        topicVo.setItems(topics);
        return topicVo;
    }

    @Override
    public Topic topicCreate(Topic topic) {
        topicMapper.insertSelective(topic);
        Topic topicVo = topicMapper.selectByPrimaryKey(topic.getId());
        return topicVo;
    }

    @Override
    public Topic topicUpdate(Topic topic) {
        topicMapper.updateByPrimaryKeySelective(topic);
        Topic topicVo = topicMapper.selectByPrimaryKey(topic.getId());
        return topicVo;
    }

    @Override
    public void topicDelete(Topic topic) {
        topic.setDeleted(true);
        topicMapper.updateByPrimaryKeySelective(topic);
    }
}
